<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px" class="queryForms">
			<el-form-item label="产品名称" prop="productName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productName" placeholder="请输入记录编号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品型号" prop="productModel">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="返修任务单" prop="repairNumber" label-width="90px">
				<el-input maxlength="20" show-word-limit v-model="queryParams.repairNumber" placeholder="请输入返修任务单号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品编号" prop="productNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productNumber" placeholder="请输入产品编号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="军检状态" prop="armyExamineStatus">
				<el-select v-model="queryParams.armyExamineStatus" placeholder="请选择">
					<el-option v-for="dict in dict.type.army_unqulilist" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="审理状态" prop="hearStatus">
				<el-select v-model="queryParams.hearStatus" placeholder="请选择">
					<el-option v-for="dict in dict.type.hear_status" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5"><div style="line-height: 30px">生成表单</div></el-col>
			<el-col :span="1.5">
				<el-select v-model="testval">
					<el-option :key="1" label="不合格审理单" :value="1" />
				</el-select>
			</el-col> -->
			<CreateForm :ids="ids" :rows="rows" formtype="t_unqualifiedList" />
			<right-toolbar @changeTableData="changeTableData" menuNames="unqualifiedList6" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="army:unqualifiedList:index"></right-toolbar>
		</el-row>

		<!-- lazy :load="loadListDes" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" -->
		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="NostandardList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<!-- <el-table-column label="记录编号" align="center" prop="militaryInspectionNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" /> -->
				<el-table-column label="返修任务单" align="center" prop="repairNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="产品代表" align="center" prop="productRepresentativeName" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="用户" align="center" prop="clientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="产品名称" align="center" prop="productName" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="产品型号" align="center" prop="productModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="产品编号" align="center" prop="productNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="军检状态" align="center" prop="armyExamineStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.army_unqulilist" :value="scope.row.armyExamineStatus" />
					</template>
				</el-table-column>
				<el-table-column label="审理状态" align="center" prop="hearStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[8].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.hear_status" :value="scope.row.hearStatus" />
					</template>
				</el-table-column>
				<el-table-column label="备注" align="center" prop="remark" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px" fixed="right">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleNones(scope.row)" v-hasPermi="['Nostandard:Nostandard:edit']">不合格原因</el-button>
						<el-button size="mini" v-if="scope.row.hearStatus == 0" type="text" icon="el-icon-s-check" @click="handleHear(scope.row)" v-hasPermi="['Nostandard:Nostandard:chenck']">审理</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleAnnex(scope.row)" v-hasPermi="['Nostandard:Nostandard:file']">附件</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination7" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 核验弹框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="85px">
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="返修任务单" prop="repairNumber">
							<el-input disabled v-model="form.repairNumber" placeholder="请输入返修任务单" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="产品代表" prop="productRepresentativeName">
							<el-input disabled v-model="form.productRepresentativeName" placeholder="请输入产品代表" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="用户" prop="clientName">
							<el-input disabled v-model="form.clientName" placeholder="请输入用户" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="产品名称" prop="productName">
							<el-input disabled v-model="form.productName" placeholder="请输入产品名称" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="产品型号" prop="productModel">
							<el-input disabled v-model="form.productModel" placeholder="请输入产品型号" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="产品编号" prop="productNumber">
							<el-input disabled v-model="form.productNumber" placeholder="请输入产品编号" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div style="height: 60vh; overflow-y: auto">
				<el-table :default-expand-all="expandAlls" :data="form.hearList" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
					<el-table-column prop="technologyName" label="工艺流程" :show-overflow-tooltip="true" min-width="160"></el-table-column>
					<el-table-column prop="processName" label="工序名称" :show-overflow-tooltip="true" min-width="160"></el-table-column>
					<el-table-column prop="reworkStatus" label="是否返修/返工" :show-overflow-tooltip="true" min-width="160" align="center">
						<template slot-scope="scope" v-if="scope.row.processName">
							<el-select v-model="scope.row.reworkStatus" @change="selectWorkRepaer(scope.row, $event)" clearable="">
								<el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name"></el-option>
							</el-select>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitData">确认</el-button>
				<el-button @click="handlescrap">报废</el-button>
				<el-button @click="handleconcession">让步接收</el-button>
				<el-button @click="open = false">取消</el-button>
			</div>
		</el-dialog>
		<el-dialog title="不合格原因" :visible.sync="openNones" width="900px" append-to-body v-dialogDrag>
			<div style="height: 65vh; overflow-y: auto">
				<el-table :data="noneList" v-loading="loadingNone" border>
					<el-table-column label="序号" align="center" type="index" width="55" />
					<el-table-column label="检查项目" prop="projectName" min-width="150" :show-overflow-tooltip="true" />
					<el-table-column label="技术要求及合格判定依据" prop="basis" min-width="200" :show-overflow-tooltip="true" />
					<el-table-column label="检查结果" prop="results" min-width="150" :show-overflow-tooltip="true" />
					<el-table-column label="备注" prop="remark" min-width="150" :show-overflow-tooltip="true" />
				</el-table>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { reworkRepair, scrap, concession, queryProcedure, examineBelowList, DisqualificationList, listNostandard, listNostandardChild, delNostandard, submitNostandard } from "@/api/army/unqualifiedList"
import { mapGetters } from "vuex"
import CreateForm from "@/views/components/CreateForm"
import { getcolumn } from "@/api/selectList"
export default {
	name: "UnqualifiedList",
	dicts: ["army_unqulilist", "hear_status"],
	components: { CreateForm },
	data() {
		return {
			expandAlls: true,
			typeList: [
				{
					id: "1",
					name: "返修"
				},
				{
					id: "2",
					name: "返工"
				}
			],
			hearList: [],
			// 是否展开，默认全部折叠
			isExpandAll: false,
			// 重新渲染表格状态
			refreshTable: true,
			noneList: [],
			loadingNone: false,
			openNones: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 选中数组列表
			rows: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 军检记录表格数据
			NostandardList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				repairId: undefined,
				militaryInspectionNumber: undefined,
				productName: undefined,
				productModel: undefined,
				armyExamineStatus: undefined,
				hearStatus: undefined,
				repairNumber: undefined,
				productNumber: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			testval: undefined,
			// 更新树刷新
			treeMap: new Map(),
			hearId: "",
			// 列信息
			columns: [
				// { key: 0, label: `记录编号`, visible: true },
				{ key: 1, label: `返修任务单`, visible: true },
				{ key: 2, label: `产品代表`, visible: true },
				{ key: 3, label: `用户`, visible: true },
				{ key: 4, label: `产品名称`, visible: true },
				{ key: 5, label: `产品型号`, visible: true },
				{ key: 6, label: `产品编号`, visible: true },
				{ key: 7, label: `军检状态`, visible: true },
				{ key: 8, label: `审理状态`, visible: true },
				{ key: 9, label: `备注`, visible: true }
			],
			repearlist: []
		}
	},
	computed: {
		...mapGetters(["sidebar"])
	},

	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination7" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		submitData() {
			let data = []
			this.repearlist.forEach(item => {
				if (item.id && item.reworkStatus) {
					data.push({
						processId: item.id,
						reworkStatus: item.reworkStatus
					})
				}
			})
			reworkRepair({
				armyExamineDisqualificationId: this.hearId,
				reworkRepairList: data
			}).then(res => {
				this.$modal.msgSuccess("审理成功")
				this.open = false
				this.getList()
			})
		},
		selectWorkRepaer(row) {
			let flag = true
			this.repearlist.length &&
				this.repearlist.forEach((item, index) => {
					if (item.id == row.id) {
						flag = false
						this.repearlist.splice(index, 1, row)
						console.log(this.repearlist)
					}
				})
			if (flag) {
				this.repearlist.push(row)
			}
			console.log(this.repearlist)
		},
		handlescrap() {
			scrap(this.hearId).then(res => {
				this.$modal.msgSuccess("审理成功")
				this.open = false
				this.getList()
			})
		},
		handleconcession() {
			concession(this.hearId).then(res => {
				this.$modal.msgSuccess("审理成功")
				this.open = false
				this.getList()
			})
		},

		/** 展开/折叠操作 */
		toggleExpandAll() {
			this.refreshTable = false
			this.isExpandAll = !this.isExpandAll
			this.$nextTick(() => {
				this.refreshTable = true
			})
		},
		handleNones(row) {
			this.loadingNone = true
			examineBelowList({
				armyExamineProductId: row.armyExamineProductId
			}).then(res => {
				this.noneList = res.rows
				this.loadingNone = false
				this.openNones = true
			})
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询军检记录列表 */
		getList() {
			this.loading = true
			DisqualificationList(this.queryParams).then(response => {
				this.NostandardList = response.rows
				this.NostandardList.forEach(item => {
					this.$refs.table.toggleRowExpansion(item, false)
				})

				this.total = response.total
				this.loading = false
			})
		},

		// 获取列表子菜单
		async loadListDes(tree, treeNode, resolve) {
			if (!this.treeMap.has(tree.id)) {
				this.treeMap.set(tree.id, {
					treeNode,
					resolve
				})
			}
			let ret = await listNostandardChild(tree.id)
			ret.rows.forEach(item => {
				// 父子id会冲突
				item.id = "c" + item.id
			})
			resolve(ret.rows)
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				data: undefined,
				remark: undefined,
				productName: undefined,
				productModel: undefined,
				armyExamineStatus: undefined,
				hearStatus: undefined
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.rows = selection
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加军检记录"
		},
		// 审理按钮操作
		handleHear(row) {
			this.reset()
			this.title = "审理"
			this.repearlist = []
			this.hearId = row.id
			queryProcedure(row.id).then(res => {
				this.open = true
				this.form = res
				this.form.hearList = res.data
			})
		},
		/** 核验操作 */
		async handleCheck(row = undefined) {
			let data = {
				armyExamineProductPaVoList: []
			}
			if (!row) {
				// 多条记录
				if (this.ids.length == 0) {
					this.$modal.msg("请选择产品")
					return false
				} else {
					// 判断是否选中了父级返修任务
					let checkp = this.rows.find(item => {
						return Number(item.id) > 0
					})
					if (checkp) {
						this.$modal.msg("只能选择子类产品")
						return false
					}
					// 判断选中的产品列表是否是同一个产品型号
					let modal = undefined
					let checkstatus = false
					let check = this.rows.every(e => {
						if (e.armyExamineStatus != "1") {
							checkstatus = true
						}
						let id = e.id.replace("c", "")
						data.armyExamineProductPaVoList.push({
							id: id,
							armyExamineStatus: undefined,
							remark: undefined
						})
						if (!e.productModel) {
							return false
						} else {
							if (!modal) {
								modal = e.productModel
								return true
							} else {
								return modal == e.productModel
							}
						}
					})
					if (!check) {
						this.$modal.msg("请选择同一产品型号的生产计划")
						return false
					}
					if (checkstatus) {
						this.$modal.msg("请选择待核验的产品")
						return false
					}
				}
			} else {
				// 单条记录
				this.isOne = true
				let id = row.id.replace("c", "")
				data.armyExamineProductPaVoList.push({
					id: id,
					armyExamineStatus: undefined,
					remark: undefined
				})
			}
			this.reset()
			this.open = true
			this.title = "核验"
			this.form.data = data
		},
		/** 提交按钮 */
		submitForm(armyExamineStatus) {
			let data = this.form.data
			data.armyExamineProductPaVoList.forEach(item => {
				item.armyExamineStatus = armyExamineStatus
				item.remark = this.form.remark
			})
			const loading = this.$loading({
				lock: true,
				text: "Loading",
				spinner: "el-icon-loading",
				background: "rgba(0, 0, 0, 0.7)"
			})
			submitNostandard(this.form.data)
				.then(response => {
					this.$modal.msgSuccess("修改成功")
					this.open = false
					loading.close()
					// 刷新树
					this.treeMap.forEach((item, idx) => {
						this.loadListDes({ id: idx }, item.treeNode, item.resolve)
					})
				})
				.finally(() => {
					loading.close()
				})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delNostandard(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		// 附件
		handleAnnex(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 47,
					perm: "Nostandard:Nostandard:file"
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 240px !important;
}
</style>
